/* Copyright 2012 Dustin Mitchell <dmmitche [at] gmail [dot] com>
 *
 * Licensed under the GPLv3.  See included LICENSE.txt for full details.
 */

#include <stdlib.h>
#include <string.h>

#include "iterator.h"

int iter_create (struct iter *iter, struct iter_ops *ops)
{
  if (!iter || !ops)
    return -1;

  if (!ops->next || !ops->prev
      || !ops->compare || !ops->deref)
    return -1;

  memcpy (&iter->ops, ops, sizeof (iter->ops));

  return 0;
}

void iter_next (struct iter *iter)
{
  if (!iter)
    return;

  if (!iter->ops.next)
    return;

  iter->ops.next (iter);
}

void iter_prev (struct iter *iter)
{
  if (!iter)
    return;

  if (!iter->ops.prev)
    return;

  iter->ops.prev (iter);
}

int iter_compare (struct iter *iter1, struct iter *iter2)
{
  if (!iter1 || !iter2)
    return 0;

  if (!iter1->ops.compare || !iter2->ops.compare)
    return 0;

  if (iter1->ops.compare != iter2->ops.compare)
    return 0;

  return iter1->ops.compare (iter1, iter2);
}

void * iter_deref (struct iter *iter)
{
  if (!iter)
    return NULL;

  if (!iter->ops.deref)
    return NULL;

  return iter->ops.deref (iter);
}
